#include "atom.h"
#include "configuration.h"
#include "reference.h"
#include "trail.h"

void Reference::bound (Node &n, Trail &trail) {
  Node *e1 = final (), *e2 = n.final ();
  if(e1 == e2)
    return; // We are approaching same target. Don't create loops!

  Reference &finalReference = static_cast<Reference&> (*e1);

  // Logs down the binding
  trail.record (finalReference);
  finalReference.target = e2;
}

pNode Reference::replicate () {
  if(target)
    return final ()->replicate ();
  else
    return Atom::create (variableName ()); // De-generalize
}
